//
//  StudentListViewController.m
//  UI11_Address
//
//  Created by dllo on 15/10/23.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "StudentListViewController.h"
#import "Singleton.h"
#import "Students.h"
#import "StudentListCell.h"
#import "StudentRightCell.h"
#import "ChineseToPinyin.h"
#import "StudentDetailViewController.h"
#import "StudentAddViewController.h"
@interface StudentListViewController ()<UITableViewDelegate,UITableViewDataSource,SDVCDelegate,StuAddVCDelegate>

@property (nonatomic, retain)UITableView *myTableView;

@end

@implementation StudentListViewController

- (void)dealloc
{
    [_myTableView release];
    [super dealloc];
}


/**
 *1.建工程,分析工程有几个页面,按照MVC设计模式,创建文件加,V和C在一个文件夹下
 *2.yinw多个页面可能共用一个model类,多以model单独放一个文件夹下
 *3.指定根视图,搭建根视图页面
 *4.页面数据先用死数据显示,检查是否可以运行
 *5.(从网络或者本地)获取数据,处理数据,显示在页面上
 */



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //刷新
    [self.myTableView reloadData];
//    [self setAutomaticallyAdjustsScrollViewInsets:NO];

    
    //注册cell
#pragma mark -- 自定义cell 第5步:注册自定义的cell
    //左边头像 boyCell
    [self.myTableView registerClass:[StudentListCell class] forCellReuseIdentifier:@"boycell"];
    
    //右边头像 girlCell
    [self.myTableView registerClass:[StudentRightCell class] forCellReuseIdentifier:@"girlcell"];
    
    //取消边界反弹效果
//    self.myTableView.bounces = NO;
    
    //取消每行横线
    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    //导航栏右侧按钮
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
    
    //设置导航栏标题
    self.navigationItem.title = @"通讯录";
    
    
   //设置通讯录列表导航栏左键
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(leftButton:)];
    
    
}

#pragma mark -- 创建TableView

//重写getter方法  创建TableView
-(UITableView *)myTableView
{
    if (_myTableView == nil) {
        
   
    self.myTableView = [[UITableView alloc]initWithFrame:[[UIScreen mainScreen]bounds] style: UITableViewStylePlain];
        self.myTableView.backgroundColor =[UIColor colorWithRed:0.616 green:1.000 blue:0.905 alpha:1.000];
        self.myTableView.delegate = self;
        self.myTableView.dataSource = self;
        [self.view addSubview:self.myTableView];
        [_myTableView release];
    }
    return _myTableView;
}

//设置有几个区
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [Singleton shareSingleton].allKeysArray.count;
}


//设置每区有几行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSString *key = [[Singleton shareSingleton].allKeysArray objectAtIndex:section];
    NSMutableArray *array = [[Singleton shareSingleton].studentDic objectForKey:key];
    return array.count;
}

//设置cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *boyCellIdentifier = @"boycell";
    static NSString *girlCellIdentifier = @"girlcell";
    
#pragma mark -- 自定义cell 第4步:替换系统的UITableViewCell
    StudentListCell *boyCell = [tableView dequeueReusableCellWithIdentifier:boyCellIdentifier];
    StudentRightCell *girlCell = [tableView dequeueReusableCellWithIdentifier:girlCellIdentifier];
    
    NSString *key = [[Singleton shareSingleton].allKeysArray objectAtIndex:indexPath.section];
    NSMutableArray *array = [[Singleton shareSingleton].studentDic objectForKey:key];
    
    Students *stu = [array objectAtIndex:indexPath.row];
    
    
#pragma mark -- 自定义cell 第6步:给cell传值
    if ([stu.gender isEqualToString:@"男"]){
        boyCell.aStudent = stu;
        return  boyCell;
        }else
            girlCell.aStudent = stu;
            return girlCell;
}


//设置行高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 110;
}


//设置字母检索
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    return [Singleton shareSingleton].allKeysArray;
}


//设置section表头标题
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    NSString *key = [[Singleton shareSingleton].allKeysArray objectAtIndex:section];
    return key;
}
//系统编辑按钮触发的方法
-(void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    
    //设置tableview编辑状态, 随按钮状态改变而改变
    [self.myTableView setEditing:editing animated:animated];
}



//询问哪行需要编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}


//每行的编辑样式(删除)

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}

//实现编辑逻辑
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    //如果是进行删除操作
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
        //特别注意:一定要先删除数据 后删cell
        NSString *key = [[Singleton shareSingleton].allKeysArray objectAtIndex:indexPath.section];
        NSMutableArray *array = [[Singleton shareSingleton].studentDic objectForKey:key];
        
        //根据row删除学生对象
        [array removeObjectAtIndex:indexPath.row];
        
        //删除cell
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationBottom];
        
        //删除整个section
        if (array.count == 0) {
            
            //删除key对应的数组
            [[Singleton shareSingleton].studentDic removeObjectForKey:key];
            [[Singleton shareSingleton].allKeysArray removeObjectAtIndex:indexPath.section];
            
            //删除section
            [tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationBottom];
        }
    }
}

//设置删除按钮文字
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
}


//设置跨区不能移动
-(NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{
    //如果移动的源section根目标section是同一个区,那么允许移动
    if (sourceIndexPath.section == proposedDestinationIndexPath.section) {
        return proposedDestinationIndexPath;
    }
    return sourceIndexPath;
}


//触摸cell时候触发方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //选中时又置灰效果, 离开时消失选中效果
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    
    
    //创建StudentDetailViewController 中的ViewController
    StudentDetailViewController *studentDVC = [[StudentDetailViewController alloc]init];
    
    //找到学生信息
    NSString *key = [[Singleton shareSingleton].allKeysArray objectAtIndex:indexPath.section];
    NSMutableArray *array = [[Singleton shareSingleton].studentDic objectForKey:key];
    Students *stu = [array objectAtIndex:indexPath.row];
    
    //把学生信息传给 详情界面创建的 学生
    studentDVC.dvStudent = stu;
    studentDVC.dvIndexPath = indexPath;
    
     //设置详情界面SDVC的代理人
    studentDVC.sdVCDelegate = self;
    NSLog(@"%@",studentDVC.dvStudent.name );
    [self.navigationController pushViewController:studentDVC animated:YES];
    [studentDVC release];
}

//移动
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
    NSString *key = [[Singleton shareSingleton].allKeysArray objectAtIndex:sourceIndexPath.section];
    NSMutableArray *array = [[Singleton shareSingleton].studentDic objectForKey:key];
    
    //交换数组中两个对象的位置
    [array exchangeObjectAtIndex:sourceIndexPath.row withObjectAtIndex:destinationIndexPath.row];
}


//详情界面协议方法
-(void)presentToController
{
    //全部刷新
    [self.myTableView reloadData];
}

//导航栏左键按钮方法
- (void)leftButton:(UIButton *)lBtn
{
    StudentAddViewController *studentAddVC = [[StudentAddViewController alloc]init];
    
    //设置添加界面sAddVC的代理人
    studentAddVC.stuAddVCDeleagte = self;
    [self.navigationController pushViewController:studentAddVC animated:YES];
}

//添加界面协议方法
-(void)presentToViewController
{
    [self.myTableView reloadData];
}

//让头标题不跟着滚
//- (void)scrollViewDidScroll:(UIScrollView *)scrollView
//{
//    CGFloat sectionHeaderHeight = 40;
//    if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0)
//    {
//        scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
//    }
//    else if (scrollView.contentOffset.y>=sectionHeaderHeight)
//    {
//        scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
//    }
//}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
